<template>
  <div class="gallery">
    <section class="gallery-header">
      <h1>作品画廊</h1>
      <div class="filter-buttons">
        <button 
          v-for="category in categories" 
          :key="category" 
          :class="{ active: currentFilter === category }"
          @click="filterWorks(category)"
          v-text="category"
        >
        </button>
      </div>
    </section>
    
    <section class="works-grid">
      <div 
        class="work-card" 
        v-for="work in filteredWorks" 
        :key="work.id"
        @click="openPreview(work)">
        <img :src="work.image" :alt="work.title">
        <div class="work-info">
          <h3 v-text="work.title"></h3>
          <p v-text="work.category"></p>
        </div>
      </div>
    </section>
    
    <!-- 图片预览模态框 -->
    <div v-if="previewWork" class="preview-modal" @click="closePreview">
      <div class="preview-content" @click.stop>
        <span class="close-btn" @click="closePreview">&times;</span>
        <img :src="previewWork.image" :alt="previewWork.title">
        <div class="preview-info">
          <h3 v-text="previewWork.title"></h3>
          <p v-text="previewWork.description"></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const categories = ['全部', '摄影', '设计', '绘画']
const currentFilter = ref('全部')
const previewWork = ref(null)

const allWorks = [
  {
    id: 1,
    title: '城市夜景',
    description: '都市夜晚的繁华与宁静',
    category: '摄影',
    image: 'https://picsum.photos/seed/g1/800/600'
  },
  {
    id: 2,
    title: '自然风光',
    description: '大自然的壮丽与和谐',
    category: '摄影',
    image: 'https://picsum.photos/seed/g2/800/600'
  },
  {
    id: 3,
    title: '人物肖像',
    description: '捕捉人物的内心世界',
    category: '摄影',
    image: 'https://picsum.photos/seed/g3/800/600'
  },
  {
    id: 4,
    title: '品牌设计',
    description: '为某品牌设计的视觉形象',
    category: '设计',
    image: 'https://picsum.photos/seed/g4/800/600'
  },
  {
    id: 5,
    title: '插画作品',
    description: '数字插画创作',
    category: '设计',
    image: 'https://picsum.photos/seed/g5/800/600'
  },
  {
    id: 6,
    title: '抽象绘画',
    description: '色彩与形式的探索',
    category: '绘画',
    image: 'https://picsum.photos/seed/g6/800/600'
  }
]

const filteredWorks = computed(() => {
  if (currentFilter.value === '全部') {
    return allWorks
  }
  return allWorks.filter(work => work.category === currentFilter.value)
})

const filterWorks = (category) => {
  currentFilter.value = category
}

const openPreview = (work) => {
  previewWork.value = work
}

const closePreview = () => {
  previewWork.value = null
}
</script>

<style scoped>
.gallery {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.gallery-header {
  text-align: center;
  margin-bottom: 40px;
}

.gallery-header h1 {
  font-size: 2.8rem;
  margin-bottom: 25px;
  color: #333;
}

.filter-buttons {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
}

.filter-buttons button {
  padding: 10px 20px;
  background-color: #f1f1f1;
  border: none;
  border-radius: 30px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 500;
}

.filter-buttons button:hover,
.filter-buttons button.active {
  background-color: #42b983;
  color: white;
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.works-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 30px;
}

.work-card {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.4s ease;
  background: white;
}

.work-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.15);
}

.work-card img {
  width: 100%;
  height: 250px;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.work-card:hover img {
  transform: scale(1.05);
}

.work-info {
  padding: 20px;
}

.work-info h3 {
  margin: 0 0 10px 0;
  font-size: 1.3rem;
  color: #333;
}

.work-info p {
  color: #6c757d;
  margin: 0;
  font-weight: 500;
}

.preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.9);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
  backdrop-filter: blur(5px);
}

.preview-content {
  background-color: white;
  border-radius: 12px;
  max-width: 90%;
  max-height: 90%;
  overflow: auto;
  position: relative;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
}

.close-btn {
  position: absolute;
  top: 15px;
  right: 20px;
  font-size: 2.5rem;
  cursor: pointer;
  color: #333;
  z-index: 1001;
  transition: color 0.3s ease;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.close-btn:hover {
  color: #42b983;
  background-color: rgba(0, 0, 0, 0.05);
}

.preview-content img {
  width: 100%;
  display: block;
  border-radius: 12px 12px 0 0;
}

.preview-info {
  padding: 25px;
}

.preview-info h3 {
  margin-top: 0;
  font-size: 1.8rem;
  color: #333;
}

.preview-info p {
  font-size: 1.1rem;
  line-height: 1.6;
  color: #555;
}
</style>